/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import DataBase.ServicioMenu;
import Logic.Clasificacion;
import Logic.DataBaseLink;
import Logic.Log;
import Logic.Regla;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author LiN
 */
public class Control {
    private static Control instance;
    private String user;
    private String pass;
    private String ip;
    private String port;
    private String db;
    private ArrayList<Regla> reglas;
    private String SELECTED_SERVER;
    private Clasificacion SELECTED_CLASIFICACION;
    private ArrayList<Clasificacion> clasificaciones;
    private ArrayList<DataBaseLink> dblinks;
    private ArrayList<Log> logs;
    
    private Control(String user,String pass,String ip,String port,String db){        
        this.user=user;
        this.pass=pass;
        this.ip=ip;
        this.port=port;
        this.db=db;
        this.SELECTED_SERVER="";
        clasificaciones=new ArrayList();
        this.levantarlista();
        
        dblinks=new ArrayList();
        this.load_links();
        System.out.println(dblinks.size()+" loaded");
        
        logs = new ArrayList();
    }
    
    public void insertLog(Log l){
        logs.add(l);
    }
    
    public void saveLogs(){
        FileOutputStream fout;
        try {
            fout = new FileOutputStream("Logs.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fout);   
            oos.writeObject(logs);
            oos.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }     
    }
    public void loadLogs(){
        clasificaciones.clear();
        FileInputStream fin;
        File file=new File("Logs.txt");
        if(file.exists()){
            try {
                fin = new FileInputStream("Logs.txt");
                ObjectInputStream ois = new ObjectInputStream(fin);
                logs = (ArrayList<Log>) ois.readObject();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException | ClassNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public void save_links(){
        FileOutputStream fout;
        try {
            fout = new FileOutputStream("DBLinks.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fout);   
            oos.writeObject(dblinks);
            oos.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    public void load_links(){
        clasificaciones.clear();
        FileInputStream fin;
        File file=new File("DBLinks.txt");
            if(file.exists()){
            try {
                fin = new FileInputStream("DBLinks.txt");
                ObjectInputStream ois = new ObjectInputStream(fin);
                dblinks=(ArrayList<DataBaseLink>) ois.readObject();
                //System.out.println(configuration.getDEFAULT_LOG());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException | ClassNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public DataBaseLink getDBLink(String name){
        Iterator ite=dblinks.iterator();
        DataBaseLink aux=null;
        while(ite.hasNext()){
            aux=(DataBaseLink) ite.next();
            if(aux.getName().equals(name)){
                return aux;
            }
        }
        return null;
    }
    public void save_clasification(){
        FileOutputStream fout;
        try {
            fout = new FileOutputStream(this.SELECTED_SERVER+"_Clasification.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fout);   
            oos.writeObject(clasificaciones);
            oos.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
        }     
    }
    
    public void load_clasification(){
        clasificaciones.clear();
        FileInputStream fin;
        File file=new File(this.SELECTED_SERVER+"_Clasification.txt");
            if(file.exists()){
            try {
                fin = new FileInputStream(this.SELECTED_SERVER+"_Clasification.txt");
                ObjectInputStream ois = new ObjectInputStream(fin);
                clasificaciones = (ArrayList<Clasificacion>) ois.readObject();
                //System.out.println(configuration.getDEFAULT_LOG());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException | ClassNotFoundException ex) {
                Logger.getLogger(Control.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static Control getInstance(String user,String pass,String ip,String port,String db){
        if(instance==null)
            instance=new Control(user,pass,ip,port,db);
        return instance;
    }

    public ArrayList<Log> getLogs() {
        return logs;
    }
    
    
    public static Control getInstance(){
        return instance;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getSELECTED_SERVER() {
        return SELECTED_SERVER;
    }

    public void setSELECTED_SERVER(String SELECTED_SERVER) {
        this.SELECTED_SERVER = SELECTED_SERVER;
    }
    
    public ServicioMenu getServicioMenu(){
        if(this.getSELECTED_SERVER().equals("LOCAL") || this.getSELECTED_SERVER().equals(""))
            return new ServicioMenu(user,pass,ip,port,db);
        else{
            DataBaseLink aux=this.getDBLink(this.getSELECTED_SERVER());
            //System.out.println(aux.getIp());
            return new ServicioMenu(aux.getUser()+" as sysdba",aux.getPass(),aux.getIp(),aux.getPort(),aux.getBd());
        }
    }

    public ArrayList<Clasificacion> getClasificaciones() {
        return clasificaciones;
    }
    
    public Clasificacion getClasificacion(String name){
        Iterator it=clasificaciones.iterator();
        while(it.hasNext()){
            Clasificacion aux=(Clasificacion) it.next();
            if(aux.getNombreRol().equals(name))
                return aux;
        }
        return null;
    }
    
    public void setClasificaciones(ArrayList<Clasificacion> clasificaciones) {
        this.clasificaciones = clasificaciones;
    }

    public Clasificacion getSELECTED_CLASIFICACION() {
        return SELECTED_CLASIFICACION;
    }

    public void setSELECTED_CLASIFICACION(Clasificacion SELECTED_CLASIFICACION) {
        this.SELECTED_CLASIFICACION = SELECTED_CLASIFICACION;
    }
    
    public void levantarlista() {
        setReglas((ArrayList<Regla>) new ArrayList());
        try {
            Regla s = new Regla();
            FileInputStream fis = new FileInputStream("reglas.txt");
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(fis);
                s = (Regla) ois.readObject();

                if (s != null) {
                    getReglas().add(s);
                }

            }

        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error levantar 1");
        } catch (IOException | ClassNotFoundException ex) {
        }
    }

    
    public ArrayList<Regla> getReglas() {
        return reglas;
    }

    
    public void setReglas(ArrayList<Regla> reglas) {
        this.reglas = reglas;
    }
    
    public void actualizarArchivo() throws FileNotFoundException, IOException {

        FileOutputStream fileOut = new FileOutputStream("reglas.txt", false);

        Iterator guarde = getReglas().iterator();
        while (guarde.hasNext()) {
            Regla ndts = (Regla) guarde.next();
            ndts.guardar("reglas.txt");
        }
        fileOut.close();
    }
    
    public Regla searchRule(String name){
        for (int i = 0; i < reglas.size(); i++) {
            String ser = reglas.get(i).getNombre();
            if(ser.equals(name)){
                return reglas.get(i);
            }
        }
        return null;
    }
    
    /*public void cargarTablaReglas() {

        for (int i = 0; i < ((DefaultTableModel) tRules.getModel()).getRowCount();) {
            ((DefaultTableModel) tRules.getModel()).removeRow(i);
        }

        //agrega lo que haya en el arraylist
        for (int i = 0; i < reglas.size(); i++) {
            String ser = reglas.get(i).getNombre();
            ((DefaultTableModel) tRules.getModel()).addRow(new Object[]{ser});
        }
    }*/

    public ArrayList<DataBaseLink> getDblinks() {
        return dblinks;
    }

    public void setDblinks(ArrayList<DataBaseLink> dblinks) {
        this.dblinks = dblinks;
    }
    
}
